clear
set type double
set more 1

***************************************
*NOTES
*Table 2 - export currency choice baseline
*********************************************************************

use ../../rawdata/world_bank/wb_country_indicators, clear
	rename iso2 cty_code
	collapse (mean) cpi_infl gdp_growth gdp_percap, by(cty_code)
save temp/world_bank, replace


*construct market share using full sample of exports
*only use up to 2019 because we don't have full 2020 trade data
******************************************************
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop *I
	drop if year==2020
	rename country cty_code
	drop if missing(valueX)
    	drop if missing(currency)
 	gen hs4=substr(cncode,1,4)
    	collapse (sum) value=valueX, by(vat hs4 cty_code year)
    	merge m:1 cty_code hs4 year using ../data/X_hs4_2012_2019, keep(match) nogen
    	
	gen ms=value/totxvalue	
	drop if missing(ms)
	replace ms=1 if ms>1
	collapse (mean) ms, by(vat hs4 cty_code)
	drop if cty_code=="EU"
save temp/ms, replace	
	

*emp
********
use ../data/emp_wages_annual_2012to2019, clear
	keep if year==2017|year==2018|year==2019
	collapse (mean) memp=emp, by(vat)
save temp/memp, replace

*FDI
***********
use ../../rawdata/outward_dummy, clear
	destring vat, replace
	keep if year==2017|year==2018|year==2019
	keep vat
	duplicates drop
save temp/outFDI, replace

use ../../rawdata/inward_dummy, clear
	destring vat, replace
	keep if year==2017|year==2018|year==2019
	keep vat
	duplicates drop
save temp/inFDI, replace


**********************************************************
*2. Preparing data for estimating currency use by exports
*dependent variable
**********************************************************
**********************************************************
*prepare dummy=1 if usd and zero otherwise
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
drop date *I
	rename country cty_code
	drop if missing(valueX)
    	drop if missing(currency)
	drop if cty_code=="EU"
	rename valueX value


*deal with multiple currencies (4.8% duplicates)
	gen cat=currency=="USD"
	collapse (sum) value, by(vat cncode cty_code year month cat)

	egen totvalue=sum(value), by(vat cncode cty_code year month)
	
	gen value_USD=value if cat==1
	gen sh_USD=value_USD/totvalue
	replace  sh_USD=0 if cat==0

*only keep euro obs when multiple and report value share
	duplicates tag vat cncode cty_code year month, gen(tag)
	drop if tag==1 & cat==0

	keep vat cncode cty_code year month sh_USD
save temp/I_USD, replace	
	

*euro indicator
***********************
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
drop date *I
	rename country cty_code
	drop if missing(valueX)
    	drop if missing(currency)
	drop if cty_code=="EU"
	rename valueX value


*deal with multiple currencies (4.8% duplicates)
	gen cat=currency=="EUR"
	collapse (sum) value, by(vat cncode cty_code year month cat)

	egen totvalue=sum(value), by(vat cncode cty_code year month)
	
	gen value_EUR=value if cat==1
	gen sh_EUR=value_EUR/totvalue
	replace  sh_EUR=0 if cat==0

*only keep euro obs when multiple and report value share
	duplicates tag vat cncode cty_code year month, gen(tag)
	drop if tag==1 & cat==0
	
** merging in relevant variables: phi
*using data from dofiles_data/phi_euro_2019 where missings and zeros are identified
*data averaged over 2017, 2018, 2019
**********************************************************	
	merge m:1 vat using ../data/phi_xeu_currency_2019, keep(match) nogen
	
*merge manuf indicators
	merge m:1 vat using ../data/manuf_firm, keep(match master) nogen
	gen HS6=substr(cncode,1,6)

	
*merge in market share (averaged)
	gen hs4=substr(cncode,1,4)
	merge m:1 vat hs4 cty_code using temp/ms, keep(match) nogen
		
*merging on mean competitor currency over sample period
merge m:1 vat cty_code hs4 using ../data/mcompetitor_xcurrency, keep(match master) nogen
	gen date=ym(year, month)
	format date %tm

	merge m:1 vat using temp/memp, keep(match master) nogen

merge m:1 vat using temp/inFDI, keep(match master)
	gen inFDI=_merge==3
	drop _merge
	
merge m:1 vat using temp/outFDI, keep(match master)
	gen outFDI=_merge==3
	drop _merge	

keep if manuf_firm==1	
save temp/estdata_xcurrencychoice, replace


********************************************************
*ESTIMATION
***********************************************


use temp/estdata_xcurrencychoice, clear
*drop firms that are not in export data, so exshare is zero (0.08 of sample)
	*merge m:1 vat using ../dofiles_data/temp/xshare_vat, keep(match) nogen

	gen lnmemp=ln(memp)
	drop if missing(lnmemp)
	
*merge on instrument for currency share	
*need to merge on HS2017, because instrument is in HS2012
		
	gen hs6 = substr(cncode,1,6)
	
	rename hs6 hs2017
	merge m:1 hs2017 using ../../rawdata/UN_hs2017_to_hs2012_conversion.dta, keep(1 3) nogen
	* only 3 hs2017 codes from the original file were not in the conversion table: 993024, 993027, 993099
	rename hs2012 hs6
	
	rename hs4 hs4_2017 
	gen hs4 = substr(hs6,1,4)
		
	merge m:1 hs4 cty_code using ../data/2017_2019_comtrade_US_sh_exp, keepusing(IVsh_*) keep(1 3)  nogen
	rename hs4 hs4_2012
	rename hs4_2017 hs4
	gen IVsh_other= IVsh_pegged_value- IVsh_US- IVsh_CHN
	
*merge on new instruments
	destring hs4, replace
	merge m:1 vat hs4 cty_code using ../data/comp_instruments, keep(match master) nogen
	gen lncomp_memp=ln(comp_memp)	
		
*variables
*if 100% euro then Ix=0
**************************
	*gen Ix=0 if currency=="EUR"
	*replace Ix=1 if currency~="EUR" & !missing(currency)

	
	gen Ix=1 - sh_EUR
	
	egen cp=group(hs4 cty_code)
	egen cty_num=group(cty_code)
	
save ../data/xcurrencychoice_estdata, replace	



use ../data/xcurrencychoice_estdata, clear

gen ms_mshcomp=ms*mshcompetitor_other
	gen ms_IVsh_US=ms*IVsh_US
	gen ms_IVsh_CHN=ms*IVsh_CHN
	gen ms_IVsh_other=ms*IVsh_other

*new instruments are lncomp_memp comp_mphi_xeu comp_mphi_xeu_other
	gen ms_lncomp_memp=ms*lncomp_memp
	gen ms_comp_mphi_xeu=ms*comp_mphi_xeu
		
*dum interaction for ms
	gen high=ms>=0.1 & !missing(ms)
	tab high
	gen high_mshcomp=high*mshcompetitor_other
	gen high_IVsh_US=high*IVsh_US
	gen high_IVsh_CHN=high*IVsh_CHN
	gen high_IVsh_other=high*IVsh_other
	
	gen high_lncomp_memp=high*lncomp_memp
	gen high_comp_mphi_xeu=high*comp_mphi_xeu	


*******************************
*TABLE 2
**************************************
eststo clear
 
*PART 1 - industry X destination fe + date
*only firm-level variables

*col1 - with hs4-country fe	
	eststo col1: reghdfe Ix mphi_xeu ms, absorb(cp date) cluster(vat)

*col2 - as 2 with phi_xeu split
	eststo col2: reghdfe Ix mphi_xeu_euro mphi_xeu_other mphi_xeu_missing ms, absorb(cp date) cluster(vat)
	

*col3 - as 1 with emp
	eststo col3: reghdfe Ix mphi_xeu lnmemp ms, absorb(cp date) cluster(vat)


*PART 2 - industry + destination fe + data
*add industry-destination variables

*col 4 - add competitor share + no interaction with ms
	eststo col4: reghdfe Ix mphi_xeu lnmemp ms mshcompetitor_other , absorb(cty_num hs4 date) cluster(vat)

*col 5: with instrument for competitor share - all instruments combined but no ms interaction
	eststo col5: ivreghdfe Ix mphi_xeu lnmemp ms  (mshcompetitor_other = IVsh_US IVsh_CHN IVsh_other lncomp_memp comp_mphi_xeu ), absorb(cty_num hs4 date) cluster(vat) first savefirst savefprefix(col5_fs_)


*col 6: with dummy ms interaction
	eststo col6: ivreghdfe Ix mphi_xeu lnmemp high (mshcompetitor_other high_mshcomp = IVsh_US IVsh_CHN IVsh_other high_IVsh_US high_IVsh_CHN high_IVsh_other lncomp_memp comp_mphi_xeu high_lncomp_memp high_comp_mphi_xeu ), absorb(cty_num hs4 date) cluster(vat) first savefirst savefprefix(col6_fs_)

* Export regressions
esttab col? using "../results/Table2_baseline.csv", se  b(3) se(3) r2 ar2 star(* 0.10 ** 0.05 *** 0.01) scalars("cdf Cragg-Donald F stat" "rkf Kleibergen-Paap F stat" "j Hansen J stat" "jp Hansen J stat p-val") compress replace

* Export first-stage regressions
esttab col?_fs_* using "../results/Table2_baseline_fs.csv", nodepvars se b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) compress replace

eststo clear
	
exit


